package com.yixq.soa.service.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.yixq.soa.service.user.service.RpPayProductService;
import com.yixq.soa.service.user.vo.RpPayProductVo;
import com.yixq.soa.service.web.annotation.Ver;
import com.yixq.soa.service.web.utils.BaseController;
import com.yixq.soa.service.web.utils.PageParam;

@Api(tags = "RpPayProductRestController", description = "支付产品管理")
@RestController
@RequestMapping("/user/rpPayProduct")
public class RpPayProductRestController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(RpPayProductRestController.class);
	@Autowired
	private RpPayProductService rpPayProductService;

	@ApiOperation(value = "支付产品列表", notes = "支付产品列表")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1"),
			@ApiImplicitParam(name = "page", value = "页码", paramType = "query", required = true, dataType = "Integer", defaultValue = "1", allowableValues = ""),
			@ApiImplicitParam(name = "start", value = "分页开始记录数", paramType = "query", required = true, dataType = "Integer", defaultValue = "0", allowableValues = ""),
			@ApiImplicitParam(name = "limit", value = "分页容量", paramType = "query", required = true, dataType = "Integer", defaultValue = "25", allowableValues = ""),
			@ApiImplicitParam(name = "auditStatus", value = "审核状态", paramType = "query", required = true, dataType = "Integer", defaultValue = "") })
	@RequestMapping(path = "/treeList/{ver}", method = { RequestMethod.GET, RequestMethod.POST })
	@Ver("1")
	public ResponseEntity<Object> getListByAuditStatus(String auditStatus, PageParam pageParam, HttpServletRequest request) {
		try {
			auditStatus="NO";
			List<RpPayProductVo> result = rpPayProductService.getRpPayProductVoListByAuditStatus(auditStatus, pageParam.getPageBounds());
			return ok(result, null == result ? 0L : result.size());
		} catch (Exception e) {
			logger.error("", e);
		}
		return error("系统内部数据错误", "402");
	}

	@ApiOperation(value = "创建支付产品", notes = "创建支付产品")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1") })
	@RequestMapping(path = "/createUnit/{ver}", method = { RequestMethod.GET, RequestMethod.POST })
	@Ver("1")
	public ResponseEntity<Object> createRpPayProduct(RpPayProductVo rpPayProductVo, HttpServletRequest request) {
		logger.warn("******:" + JSONObject.toJSONString(rpPayProductVo));
		String checkRes = checkData(rpPayProductVo);
		if (checkRes != null) {
			return error(checkRes);
		}
		try {
			boolean flag = rpPayProductService.insert(rpPayProductVo);
			if (flag) {
				return ok(null, "操作成功");
			} else {
				return error("操作失败", "402");
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		return error("系统内部数据错误", "402");
	}

	@ApiOperation(value = "修改支付产品", notes = "修改支付产品")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1") })
	@RequestMapping(path = "/updateRpPayProductById/{ver}", method = { RequestMethod.GET, RequestMethod.POST })
	@Ver("1")
	public ResponseEntity<Object> updateRpPayProductById(RpPayProductVo rpPayProductVo, HttpServletRequest request) {
		logger.warn("******:" + JSONObject.toJSONString(rpPayProductVo));
		String checkRes = checkData(rpPayProductVo);
		if (checkRes != null) {
			return error(checkRes);
		}
		if (StringUtils.isBlank(rpPayProductVo.getId())) {
			return error("房ID不能为空", "402");
		}
		try {
			boolean flag = rpPayProductService.updateById(rpPayProductVo);
			if (flag) {
				return ok(null, "操作成功");
			} else {
				return error("操作失败", "402");
			}

		} catch (Exception e) {
			logger.error("", e);
		}
		return error("系统内部数据错误", "402");
	}

	@ApiOperation(value = "删除支付产品信息", notes = "删除支付产品信息")
	@ApiResponses({ @ApiResponse(code = 200, message = "处理成功") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "ver", value = "接口版本", paramType = "path", required = true, dataType = "String", defaultValue = "v1"),
			@ApiImplicitParam(name = "id", value = "支付产品ID", paramType = "query", required = true, dataType = "Integer", defaultValue = "") })
	@RequestMapping(path = "/deleteRpPayProductById/{ver}", method = { RequestMethod.GET, RequestMethod.POST })
	@Ver("1")
	public ResponseEntity<Object> deleteRpPayProductById(String id, HttpServletRequest request) {
		logger.warn("******:" + id);
		try {
			boolean flag = rpPayProductService.deleteById(id);
			if (flag) {
				return ok(null, "操作成功");
			} else {
				return error("操作失败", "400");
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		return error("系统内部数据错误", "402");
	}
}
